package ru.dc.gui.swing;

import org.jdesktop.swingx.JXTable;
import ru.dc.Application;
import ru.dc.object.HubEventListener;
import ru.dc.object.SearchConstants;
import ru.dc.object.SearchRequest;
import ru.dc.object.ServerInfo;
import ru.dc.object.events.hub.HubEvent;
import ru.dc.object.events.hub.SearchRequestedEvent;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * <pre>
 * User: Penkov Vladimir
 * Date: 09.11.2010
 * Time: 13:47:12
 * </pre>
 */
public class SearchSpy extends Tab implements HubEventListener {
    private JXTable resultsTable;
    private JPanel rootPanel;
    private JPanel wrapper;
    private JCheckBox ignoreTTH;

    public SearchSpy() {

        SearchSpyTableModel model = new SearchSpyTableModel();
        resultsTable.setModel(model);
        resultsTable.setSortOrder(1, SortOrder.DESCENDING);

        ignoreTTH.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                if (ignoreTTH.isSelected()) {
                    resultsTable.setRowFilter(new RowFilter<Object, Object>() {
                        @Override
                        public boolean include(Entry<? extends Object, ? extends Object> entry) {
                            SearchSpyTableModel model = (SearchSpyTableModel) entry.getModel();
                            final String query = (String) model.getValueAt((Integer) entry.getIdentifier(), 0);
                            return !query.startsWith("TTH:");
                        }
                    });
                }
                else {
                    resultsTable.setRowFilter(null);
                }

            }
        });


        resultsTable.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount()==2 && SwingUtilities.isLeftMouseButton(e)) {
                    int viewRow = resultsTable.rowAtPoint(e.getPoint());
                    int modelRow = resultsTable.convertRowIndexToModel(viewRow);
                    SearchSpyTableModel model = (SearchSpyTableModel) resultsTable.getModel();
                    String query = (String) model.getValueAt(modelRow, 0);

                    SearchRequest req = new SearchRequest();
                    req.setQuery(query);
                    Application.getApplication().getMainFrame().search(req);
                }
            }
        });

        setLayout(new BorderLayout());
        add(rootPanel, BorderLayout.CENTER);

    }

    @Override
    public void init() {
        super.init();
        setTitle("Поисковый шпион");
        Application.getApplication().getHubEventsTranslator().addEventListener(this);
    }

    @Override
    public void close() {
        super.close();
        Application.getApplication().getHubEventsTranslator().removeEventListener(this);
    }

    public void onEvent(HubEvent e) {
        if (e instanceof SearchRequestedEvent) {
            SearchRequestedEvent ev = (SearchRequestedEvent) e;
            SearchRequest req = ev.getRequest();
            SearchSpyTableModel model = (SearchSpyTableModel) resultsTable.getModel();
            String query = req.getDataType()== SearchConstants.SearchType.TTH ? "TTH:"+req.getQuery() : req.getQuery();
            model.addResult(query);
            alert();
        }
    }
}
